<?php
/** 取首页的7日销量数据
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-10-9
 * Time: 上午9:47
 */
if(!$_SESSION['sid']) exit;
$r = curr(array('aid'=>$_SESSION['sid']));
$tid = curr_1(array('ty'=>'tid','tid'=>$r['tid']));

if(count($r['list']))
foreach($r['list'] as $k => $v){//整合同景区产品
    $title = (strlen($tid['land'][$tid['tid'][$k]]['title'])>30)?'...':'';
    $res[$tid['tid'][$k]]['title'] = mb_substr($tid['land'][$tid['tid'][$k]]['title'],0,10,'utf-8').$title;
    $res[$tid['tid'][$k]]['full_title'] = $tid['land'][$tid['tid'][$k]]['title'];
    $res[$tid['tid'][$k]]['torder'] += $v['torder'];
    $res[$tid['tid'][$k]]['tnum']   += $v['tnum'];
    $res[$tid['tid'][$k]]['money']  += $v['money'];
    $res[$tid['tid'][$k]]['pmode0'] += $v['pmode0'];
    $res[$tid['tid'][$k]]['pmode1'] += $v['pmode1'];
    $res[$tid['tid'][$k]]['pmode2'] += $v['pmode2'];
    $res[$tid['tid'][$k]]['pmode3'] += $v['pmode3'];
}

$n = 0;
$res_html = '';
if(count($res)){
    foreach($res as $v){
        $sort[] =  $v['torder'];
    }

    array_multisort($sort,SORT_DESC,$res);

    foreach($res as $k=>$v){
        ++$n;
        $v['money'] = chu100($v['money']);
        $v['pmode0'] = chu100($v['pmode0']);
        $v['pmode1'] = chu100($v['pmode1']);
        $v['pmode2'] = chu100($v['pmode2']);
        $v['pmode3'] = chu100($v['pmode3']);
        $res_html .= <<<HTML
                    <tr>
                        <td>$n</td>
                        <td class="tL" title="{$v['full_title']}">{$v['title']}</td>
                        <td>{$v['torder']}</td>
                        <td>{$v['tnum']}</td>
                        <td>{$v['money']}</td>
                        <td>{$v['pmode0']}</td>
                        <td>{$v['pmode1']}</td>
                        <td>{$v['pmode2']}</td>
                        <td>{$v['pmode3']}</td>
                    </tr>
HTML;
    }
}
echo $res_html;
function curr($query=array()){
    $query['token'] = md5($query['btime'].'76676591919293971034$&hfnf$%(,.!AebIOn924638280479623896');
    $url = "http://s.12301.cc/pft/order_report/weekSales.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1);
    return json_decode(curl_exec($ch),true);
    return curl_exec($ch);
}

function curr_1($query){
    $query['time'] = $_SERVER['REQUEST_TIME'];
    $query['token'] = md5($query['time'].'0393156885586815305050516493889730425518949732797083380078766300585');
    $url = "http://www.12301.cc/module/zax/gmm.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1);
//    return curl_exec($ch);
    return json_decode(curl_exec($ch),true);
}

function chu100($money){
    $money = $money/100;
    $money = number_format($money,2);
    return $money;
}